home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / irit5 / geom_lib / poly_cln.c < prev    next >
C/C++ Source or Header  |  1995-12-30  |  4KB  |  115 lines

  1. /******************************************************************************
  2. * Poly_cln.c - Clean polygonal data.                          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, June 1993.                          *
  5. ******************************************************************************/
  6.  
  7. #include "irit_sm.h"
  8. #include "allocate.h"
  9. #include "iritprsr.h"
  10. #include "poly_cln.h"
  11.  
  12. /*****************************************************************************
  13. * DESCRIPTION:                                                               M
  14. *   Routine to clean up polygons - delete zero length edges, and polygons    M
  15. * with less than 3 vertices.                             M
  16. *                                                                            *
  17. * PARAMETERS:                                                                M
  18. *   PPolygons:    List of polygons to clean, in place.                       M
  19. *                                                                            *
  20. * RETURN VALUE:                                                              M
  21. *   void                                                                     M
  22. *                                                                            *
  23. * KEYWORDS:                                                                  M
  24. *   CleanUpPolygonList, zero length edges, cleaning                          M
  25. *****************************************************************************/
  26. void CleanUpPolygonList(IPPolygonStruct **PPolygons)
  27. {
  28.     IPPolygonStruct *PPHead, *PPLast;
  29.     IPVertexStruct *PVHead, *PVTemp, *PVNext;
  30.  
  31.     PPLast = PPHead = *PPolygons;
  32.  
  33.     while (PPHead != NULL) {
  34.     PVHead = PVTemp = PPHead -> PVertex;
  35.     /* Look for zero length edges (V == V -> Pnext): */
  36.     do {
  37.         PVNext = PVTemp -> Pnext;
  38.         if (PT_APX_EQ(PVTemp -> Coord, PVNext -> Coord)) { /* Del PVNext.*/
  39.         PVTemp -> Pnext = PVTemp -> Pnext -> Pnext;
  40.         PVNext -> Pnext = NULL;            /* Free only PVNext. */
  41.         if (PVHead == PVNext) {          /* If we actually kill header. */
  42.             PPHead -> PVertex = PVHead = PVTemp;
  43.             break;
  44.         }
  45.         IPFreeVertexList(PVNext);
  46.         }
  47.         else
  48.         PVTemp = PVTemp -> Pnext;
  49.     }
  50.     while (PVTemp != NULL && PVTemp != PVHead);
  51.  
  52.     /* Now test if at list 3 vertices in polygon, otherwise delete it:   */
  53.     if (PVHead == PVHead -> Pnext ||         /* One vertex only. */
  54.         PVHead == PVHead -> Pnext -> Pnext) {      /* Two vertices only. */
  55.         if (PPHead == *PPolygons) {
  56.         *PPolygons = (*PPolygons) -> Pnext;
  57.         IPFreePolygon(PPHead);
  58.         PPHead = (*PPolygons);
  59.         }
  60.         else {
  61.         PPLast -> Pnext = PPHead -> Pnext;
  62.         IPFreePolygon(PPHead);
  63.         PPHead = PPLast -> Pnext;
  64.         }
  65.     }
  66.     else {
  67.         PPLast = PPHead;
  68.         PPHead = PPHead -> Pnext;
  69.     }
  70.     }
  71. }
  72.  
  73. /*****************************************************************************
  74. * DESCRIPTION:                                                               M
  75. *   Routine to clean up polylines of zero length.                 M
  76. *                                                                            *
  77. * PARAMETERS:                                                                M
  78. *   PPolylines:    List of polylines to clean, in place.                     M
  79. *                                                                            *
  80. * RETURN VALUE:                                                              M
  81. *   void                                                                     M
  82. *                                                                            *
  83. * KEYWORDS:                                                                  M
  84. *   CleanUpPolylineList, zero length polyline, cleaning                      M
  85. *****************************************************************************/
  86. void CleanUpPolylineList(IPPolygonStruct **PPolylines)
  87. {
  88.     IPPolygonStruct
  89.     *Poly = *PPolylines;
  90.  
  91.     /* Remove empty sized polylines. */
  92.     while (Poly != NULL &&
  93.        (Poly -> PVertex == NULL ||
  94.         Poly -> PVertex -> Pnext == NULL)) {
  95.     *PPolylines = (*PPolylines) -> Pnext;
  96.     IPFreePolygon(Poly);
  97.     Poly = *PPolylines;
  98.     }
  99.  
  100.     if (Poly && Poly -> Pnext) {
  101.     while (Poly -> Pnext != NULL) {
  102.         if (Poly -> Pnext -> PVertex == NULL ||
  103.         Poly -> Pnext -> PVertex -> Pnext == NULL) {
  104.         IPPolygonStruct
  105.             *TmpPoly = Poly -> Pnext;
  106.  
  107.         Poly -> Pnext = TmpPoly -> Pnext;
  108.         IPFreePolygon(TmpPoly);
  109.         }
  110.         else
  111.         Poly = Poly ->Pnext;
  112.     }
  113.     }
  114. }
  115.